/****************************************************************************
 * Copyright (c) 2023 Qualcomm Technologies International, Ltd.
****************************************************************************/
/**
 *
 * \defgroup ml_engine
 * \file  abs_kalimba_example.c
 *
 * example for a custom operator source file
 *
 */
 /****************************************************************************/

#include "abs_kalimba_public_example.h"
#include "ml_defines.h"

#define UNUSED(x) ((void)(x))


int do_abs(Tensor *in, Tensor *out, Abs *abs, int len);

/* Forward function */
int abs_forward(layer_lite *layer)
{
    Tensor *src = layer->p_input_tensors->tensors[0];
    Tensor *dst = layer->p_output_tensors->tensors[0];

    /* Read parameter from the layer */
    Abs *abs = (Abs *)layer->params;

    /* Placing this check to support inplace functionality */
    if (abs->is_inplace) {
        dst->data = src->data;
    }

    /* The logic of the operator goes here */

    int status = do_abs(src, dst, abs, src->num_elems);

    return status;
}

int do_abs(Tensor *in, Tensor *out, Abs *abs, int len)
{
    sat fract __DM1 *p_input = (sat fract __DM1 *)in->data;
    sat fract __DM2 *p_output = (sat fract __DM2 *)out->data;

#pragma loop minitercount(1)
    for (int i = 0; i < len; ++i)
    {
        if (p_input[i] < 0)
        {
            p_output[i] = -p_input[i];
        }
        else
        {
            p_output[i] = p_input[i];
        }
    }
    return ML_SUCCESS;
}
